#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <algorithm>
#define int long long
using namespace std;

void solve()
{
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<int>arr(n + 10 + n);
    for(int i = 1; i <= n; i ++) cin >> arr[i],arr[i] %= m;
    sort(arr.begin() + 1, arr.begin() + 1 + n);
    for(int i = 1; i <= n; i ++) arr[n + i] = arr[i] + m;
    vector<int>sum(n + 10 + n);
    for(int i = 1; i <= n + n; i ++) sum[i] = sum[i - 1] + arr[i];
    int ans = 1e18;
    for(int i = 1; i <= n; i ++)
    {
        int l = i;
        int r = i + n - 1;
        int mid = (l + r) / 2;
        int res = arr[mid] * (mid - l) - (sum[mid - 1] - sum[l - 1]) + (sum[r] - sum[mid]) - arr[mid] * (r - mid);
        ans = min(ans,res);
    }
    cout << ans << endl;
}

signed main()
{
    int t;
    cin >> t;
    while(t --)
    {
        solve();
    }
}
